import { Redirect, RouteProps } from "react-router-dom";
import { Route } from "react-router-dom";
import { hasToken } from "@/utils/storage";

export default function PrivateRoute({children, ...rest}: RouteProps) {
  return (
    <Route
      {...rest}
      render={({location}) => {
        if (hasToken()) {
          return children as React.ReactNode
        } else {
          return <Redirect to={{
            pathname: '/login',
            state: {from: location.pathname}
          }}/>
        }
      }}
    ></Route>)
}